Imports System.Xml.Linq

Friend Class SharePointFieldHelpers

    Friend Function XElementToField(ByVal f As XElement, ByVal site As Uri) As SharePointField

        Dim field As SharePointField

        Dim type = FieldTypeMappings.GetFieldType(f.Attribute("Type").Value)
        field = New SharePointField()

        Dim staticName = f.Attribute("StaticName").Value
        field.Name = f.Attribute("Name").Value
        Dim defaultElement = (f.Descendants(SharePointConnector.SharePointNameSpace + "Default").FirstOrDefault())
        field.DefaultValue = If(defaultElement Is Nothing, Nothing, defaultElement.Value)
        field.DisplayName = f.Attribute("DisplayName").Value
        field.Id = f.Attribute("ID").Value
        field.Type = type
        field.IsRequired = Boolean.Parse(f.AttributeOrDefault("Required", "FALSE"))
        field.IsReadOnly = Boolean.Parse(f.AttributeOrDefault("ReadOnly", "FALSE")) OrElse staticName = "ContentType"
        field.IsHidden = Boolean.Parse(f.AttributeOrDefault("Hidden", "FALSE"))
        field.MaxLength = Int32.Parse(f.AttributeOrDefault("MaxLength", "0"))

        Return field
    End Function

End Class
